package com.edu.whty.common.tools;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Date工具类
 *
 * @author Administrator
 */
public class DateUtils {

    public static String formatTime(Long ms) {
        int ss = 1000;
        int mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;
        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day).append("天");
        }
        if (hour > 0) {
            sb.append(hour).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分");
        }
        if (second > 0) {
            sb.append(second).append("秒");
        }
        if (milliSecond > 0) {
            sb.append(milliSecond).append("毫秒");
        }
        return sb.toString();
    }

    /**
     * 日期格式(yyyy/M/d)
     */
    public static final String YYYY_M_D_SOLIDUS = "yyyy/M/d";

    /**
     * 日期格式(yyyy/M/d)
     */
    public static final String YYYY_MM_dd_SOLIDUS = "yyyy/MM/dd";

    /**
     * 日期格式(yyyy-MM-dd)
     */
    public static final String yyyy_MM_dd_EN = "yyyy-MM-dd";

    /**
     * 日期格式(yyyyMMdd)
     */
    public static final String yyyyMMdd_EN = "yyyyMMdd";

    /**
     * 日期格式(yyyy-MM)
     */
    public static final String yyyy_MM_EN = "yyyy-MM";

    /**
     * 日期格式(yyyyMM)
     */
    public static final String yyyyMM_EN = "yyyyMM";

    /**
     * 日期格式(yyyy)
     */
    public static final String yyyy_EN = "yyyy";

    /**
     * 日期格式(yyyy-MM-dd HH:mm:ss)
     */
    public static final String yyyy_MM_dd_HH_mm_ss_EN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式(yyyyMMddHHmmss)
     */
    public static final String yyyyMMddHHmmss_EN = "yyyyMMddHHmmss";

    /**
     * 日期格式带毫秒(yyyyMMddHHmmssSSS)
     */
    public static final String yyyyMMddHHmmssSSS_EN = "yyyyMMddHHmmssSSS";

    /**
     * 日期格式(yyyy年MM月dd日)
     */
    public static final String yyyy_MM_dd_CN = "yyyy年MM月dd日";

    /**
     * 日期格式(yyyy年MM月dd日HH时mm分ss秒)
     */
    public static final String yyyy_MM_dd_HH_mm_ss_CN = "yyyy年MM月dd日HH时mm分ss秒";

    /**
     * 日期格式(yyyy年MM月dd日HH时mm分)
     */
    public static final String yyyy_MM_dd_HH_mm_CN = "yyyy年MM月dd日HH时mm分";

    /**
     * 日期格式(MM月dd日 HH:mm)
     */
    public static final String MM_dd_HH_mm_CN_EN = "MM月dd日 HH:mm";

    /**
     * 日期格式(MM月dd日)
     */
    public static final String MM_dd_CN = "MM月dd日";

    /**
     * 日期格式(yyyy年MM月dd日 HH:mm)
     */
    public static final String yyyy_MM_dd_HH_mm_CN_EN = "yyyy年MM月dd日 HH:mm";

    /**
     * 日期格式(HH:mm)
     */
    public static final String HH_mm_EN = "HH:mm";

    /**
     * 日期格式(HH:mm:ss)
     */
    public static final String HH_mm_ss_EN = "HH:mm:ss";

    /**
     * 日期格式(HHmmss)
     */
    public static final String HHmmss_EN = "HHmmss";

    /**
     * 日期格式(MMdd)
     */
    public static final String MMdd_EN = "MMdd";

    /**
     * 日期格式(yyyy.MM.dd)
     */
    public static final String yyyyDotMMDotdd_EN = "yyyy.MM.dd";

    /**
     * 日期格式(yyyy-MM-dd HH:mm)
     */
    public static final String yyyy_MM_dd_HH_mm_EN = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式(yyyy-MM-dd H:m)
     */
    public static final String yyyy_MM_dd_H_m_EN = "yyyy-MM-dd H:m";

    /**
     * 日期格式(MM.dd)
     */
    public static final String MMDotdd_EN = "MM.dd";

    /**
     * DateFormat缓存
     */
    private static final ConcurrentMap<String, DateFormat> dateFormatMap = new ConcurrentHashMap<>();


    /**
     * @Description
     * @Author xiongxiaofei
     * @Date 2019/12/27
     */
    public static DateFormat getDateFormat(String formatStr) {
        DateFormat dateFormat = dateFormatMap.get(formatStr);
        if (dateFormat == null) {
            dateFormat = new SimpleDateFormat(formatStr, Locale.CHINA);
            dateFormatMap.put(formatStr, dateFormat);
        }
        return dateFormat;
    }

    /**
     * 日期转换成字符串
     *
     * @param date yyyy-MM-dd HH:mm:ss
     * @return str
     */

    public static String DateToStr(Date date) {
        DateFormat format = getDateFormat(yyyy_MM_dd_HH_mm_ss_EN);
        if (date == null) {
            return null;
        } else {
            return format.format(date);
        }
    }

    /**
     * @param date
     * @param formatStr
     * @return
     */
    public static String DateToStr(Date date, String formatStr) {
        DateFormat format = getDateFormat(formatStr);
        if (date == null) {
            return null;
        } else {
            return format.format(date);
        }
    }

    /**
     * 按照默认formatStr的格式，转化dateTimeStr为Date类型 dateTimeStr必须是formatStr的形式
     *
     * @param dateTimeStr
     * @param formatStr
     * @return
     */
    public static Date getDate(String dateTimeStr, String formatStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
//            DateFormat sdf = DateUtils.getDateFormat(formatStr);
            DateFormat sdf = new SimpleDateFormat(formatStr);
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description 转化dateTimeStr为Date类型
     * @Author xiongxiaofei
     * @Date 2019/12/27
     */
    public static Date convertDate(String dateTimeStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
            DateFormat sdf = DateUtils.getDateFormat(yyyy_MM_dd_EN);
            // Date d = sdf.parse(dateTimeStr);
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description 转化dateTimeStr为Date类型
     * @Author xiongxiaofei
     * @Date 2019/12/27
     */
    public static Date convertDateSlash(String dateTimeStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
            DateFormat sdf = DateUtils.getDateFormat(YYYY_MM_dd_SOLIDUS);
            // Date d = sdf.parse(dateTimeStr);
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description 转化dateTimeStr为Date类型
     * @Author xiongxiaofei
     * @Date 2019/12/27
     */
    public static Date convertDateStr(String dateTimeStr) {
        try {
            if (dateTimeStr == null || "".equals(dateTimeStr)) {
                return null;
            }
            DateFormat sdf = DateUtils.getDateFormat(yyyy_MM_dd_HH_mm_ss_EN);
            // Date d = sdf.parse(dateTimeStr);
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 按照默认显示日期时间的格式"yyyy-MM-dd"，转化dateTimeStr为Date类型
     * dateTimeStr必须是"yyyy-MM-dd"的形式
     *
     * @param dateTimeStr
     * @return
     */
    public static Date getDate(String dateTimeStr) {
        return getDate(dateTimeStr, yyyy_MM_dd_EN);
    }

    /**
     * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
     *
     * @param date
     * @return
     */
    public static String dateToDateString(Date date) {
        return dateToDateString(date, yyyy_MM_dd_HH_mm_ss_EN);
    }

    /**
     * 将Date转换成字符串“yyyy年mm月dd日”的字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        return dateToDateString(date, yyyy_MM_dd_CN);
    }

    /**
     * 将Date转换成formatStr格式的字符串
     *
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToDateString(Date date, String formatStr) {
        if (date == null) {
            return "";
        } else {
            DateFormat dateFormat = getDateFormat(formatStr);
            return dateFormat.format(date);
        }
    }

    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(today);
    }

    public static List<String> findDates(String dBegin, String dEnd) {
        List<String> lDate = new ArrayList<>();
        int days = (int) (compareDateStr(dBegin, dEnd)) / (1000 * 3600 * 24);
        if (days > 0) {
            int i = 0;
            while (i < days) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate(dBegin));
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + i);
                Date today = calendar.getTime();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String result = format.format(today);
                lDate.add("'" + result + "'");
                i++;
            }
        }
        return lDate;
    }

    /**
     * 获取当前日期yyyy-MM-dd的形式
     *
     * @return
     */
    public static String getCurDate() {
        return dateToDateString(new Date(), yyyy_MM_dd_EN);
    }

    public static String getCurDateSlash() {
        return dateToDateString(new Date(), YYYY_MM_dd_SOLIDUS);
    }

    /**
     * 获取当前日期时间yyyy-MM-dd HH:mm:ss的形式
     *
     * @return
     */
    public static String getCurDateTime() {
        return dateToDateString(new Date(), yyyy_MM_dd_HH_mm_ss_EN);
    }

    /**
     * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期，之间相差多少毫秒,time2-time1
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long compareDateStr(String time1, String time2) {
        Date date1 = getDate(time1, yyyy_MM_dd_HH_mm_ss_EN);
        Date date2 = getDate(time2, yyyy_MM_dd_HH_mm_ss_EN);
        if (date1 != null && date2 != null) {
            return date2.getTime() - date1.getTime();
        }
        return 0L;
    }

    public static long compareDateStr(Date date1, Date date2) {
        return date2.getTime() - date1.getTime();
    }

    /**
     * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期，之间相差多少秒,time2-time1
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long compareDateStrSecond(String time1, String time2) {
        Date date1 = convertDateStr(time1);
        Date date2 = convertDateStr(time2);
        if (date1 != null && date2 != null) {
            return date2.getTime() - date1.getTime();
        }
        return 0L;
    }

    /**
     * 获取当前时间的后一天
     *
     * @param date
     * @return
     */
    public static Date getNextDay(Date date) {
        return getNextDay(date, 1);
    }

    /**
     * 获取当前时间后几天
     *
     * @param date
     * @param day
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getNextDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        // Date date1 = calendar.getTime();
        return calendar.getTime();
    }


    public static Date getNextYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);
        // Date date1 = calendar.getTime();
        return calendar.getTime();
    }

    public static Date getNextMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        // Date date1 = calendar.getTime();
        return calendar.getTime();
    }

    public static Date getNextMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        // Date date1 = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 后一天
     *
     * @param date yyyy-MM-dd
     * @return
     */
    public static String getNextDay(String date) {
        return getNextDay(date, 1);
    }

    /**
     * 后一天
     *
     * @param date
     * @param day
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static String getNextDay(String date, int day) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.DAY_OF_MONTH, day);
        // Date date1 = calendar.getTime();
        Date time = calendar.getTime();
        return format.format(time);
    }

    /**
     * 时间加减分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getNewDate(Date date, int minute) {
        long time = date.getTime() + minute * 60 * 1000;
        // Date newDate = new Date(time);
        return new Date(time);
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 根据timeMillis获取日期字符串，默认是yyyy-MM-dd形式
     *
     * @param timeMillis
     * @return
     */
    public static String getDefaultDateString(long timeMillis) {
        return getDateString(timeMillis, yyyy_MM_dd_EN);
    }

    /**
     * 根据timeMillis获取时间字符串，默认是yyyy-MM-dd HH:mm:ss形式
     *
     * @param timeMillis
     * @return
     */
    public static String getDefaultTimeString(long timeMillis) {
        return getDateString(timeMillis, yyyy_MM_dd_HH_mm_ss_EN);
    }

    /**
     * 根据timeMillis获取日期字符串，按指定的模式格式化
     *
     * @param timeMillis
     * @param pattern
     * @return
     */
    public static String getDateString(long timeMillis, String pattern) {
        return getDateFormat(pattern).format(timeMillis);
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getNowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    /**
     * 获取一周前的日期
     */
    public static String getWeekDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -7);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取日期前一天
     *
     * @param date
     * @return
     */
    public static Date getPreDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        // Date date1 = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 获取日期的
     *
     * @param date
     * @return
     */
    public static int getCurrDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                return 7;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            case 6:
                return 5;
            case 7:
                return 6;
            default:

        }
        return week;
    }

    /**
     * 获取当天是周几，星期天返回0
     * @return
     */
    public static int getcurrentWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                return 0;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            case 6:
                return 5;
            case 7:
                return 6;
            default:

        }
        return week;
    }

    public static String getWeekZh(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
            default:

        }
        return "";
    }

    public static int getWeekNum(String week) {
        switch (week) {
            case "周一":
                return 1;
            case "周二":
                return 2;
            case "周三":
                return 3;
            case "周四":
                return 4;
            case "周五":
                return 5;
            case "周六":
                return 6;
            case "周日":
                return 7;
            default:

        }
        return 0;
    }

    public static int getWeek(String date) {
        Date a = getDate(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(a);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                return 7;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            case 6:
                return 5;
            case 7:
                return 6;
            default:
        }
        return week;
    }

    public static String getWeekNmae(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
        }
        return null;
    }

    /**
     * 获取当前周第一天,中国习惯
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获取当前周第一天,中国习惯,若查询日期是周日,则显示这周一,而非下周一
     *
     * @return
     * @time 2019年4月28日
     */
    public static Date getWeekStartNew() {
        Calendar cal = Calendar.getInstance();
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }


    /**
     * 获取当前周最后一天,中国习惯
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getWeekStart());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return cal.getTime();
    }


    /**
     * 获取当前周第一天,中国习惯
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getWeekStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }


    /**
     * 获取当前周最后一天,中国习惯
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getWeekEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getWeekStart(date));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return cal.getTime();
    }

    /**
     * 获取当前月第一天
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getMonthStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }


    /**
     * 获取当前月最后一天
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.add(Calendar.DAY_OF_WEEK, -1);
        return cal.getTime();
    }

    /**
     * 获取月第一天
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getMonthStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }


    /**
     * 获取月最后一天
     *
     * @return
     * @author jclyzx
     * @time 2018年6月7日
     */
    public static Date getMonthEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.add(Calendar.DAY_OF_WEEK, -1);
        return cal.getTime();
    }

    /**
     * 判断传入的两个时间是否在同一周
     *
     * @param date1 (yyyy-MM-dd)
     * @param date2 (yyyy-MM-dd)
     * @return
     */
    public static boolean isSameDate(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(date1);
            d2 = format.parse(date2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setFirstDayOfWeek(Calendar.MONDAY);//将周一设为一周第一天
        cal2.setFirstDayOfWeek(Calendar.MONDAY);
        cal1.setTime(d1);
        cal2.setTime(d2);

        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (subYear == 0) {
            // subYear==0,说明是同一年
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) {
            //subYear==1,说明cal比cal2大一年;java的一月用"0"标识，那么12月用"11"
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11) {
            //subYear==-1,说明cal比cal2小一年
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        }

        return false;
    }


    /**
     * 获取某一周的周一日期
     * 周一按 周一 到 周日 的逻辑来计算
     *
     * @param date
     * @return
     */
    public static Date getMonDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, -6);
        } else {
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        }
        return calendar.getTime();
    }

    // 获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime();
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return weekEndSta;
    }

    // 获取上周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return cal.getTime();
    }

    // 获取上周的结束时间
    public static Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return weekEndSta;
    }

    // 获取上月的开始时间
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        calendar.set(Integer.valueOf(gc.get(1)), gc.get(2) + 1 - 2, 1);
        return calendar.getTime();
    }

    // 获取上月的结束时间
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        calendar.set(Integer.valueOf(gc.get(1)), gc.get(2) + 1 - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(Integer.valueOf(gc.get(1)), gc.get(2) + 1 - 2, day);
        return calendar.getTime();
    }


    /**
     * 判断时间是否在时间列表内
     * @param nowTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, List<Date> list) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = sdf.format(nowTime);
        Date new1 = sdf.parse(s1);

        Set<Date> stringSet = new HashSet<>(list);
        if (stringSet.contains(new1)) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * long转时间字符串
     * @param lo
     * @return
     */
    public static String longToDate(long lo){

        long hour = lo / 60 / 60;
        String h1 = Long.toString(hour);
        if (hour < 10){
            h1 = 0 + Long.toString(hour);
        }

        //分
        long minutes = lo / 60 % 60;
        String m1 = Long.toString(minutes);
        if (minutes < 10){
            m1 = 0 + Long.toString(minutes);
        }

        //秒
        long remainingSeconds = lo % 60;
        String s1 = Long.toString(remainingSeconds);
        if (remainingSeconds < 10){
            s1 = 0 + Long.toString(remainingSeconds);
        }

        return h1 + ":" + m1 +":" +s1;
    }

    /**
     * 判断日期是否在日期范围内,YYYY-MM-DD格式的比较
     * @param nowDate
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static boolean beInDateRange(String nowDate, String startDate,String endDate) {

        if(StringUtils.isAnyEmpty(nowDate,startDate,endDate)){
            return false;
        }

        //等于首尾直接返回true
        if(nowDate.equals(startDate) || nowDate.equals(endDate)){
            return true;
        }

        try {

            Date now = getDate(nowDate);
            Date start = getDate(startDate);
            Date end = getDate(endDate);

            if(now.after(start) && now.before(end)){
                return true;
            }

        }catch (Exception e){
            //数据不合法时返回flase
        }

        return false;

    }

    /**
     * 获取当前年的第一天
     * @return
     */
    public static Date getYearFirst() {

        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);

        calendar.clear();

        calendar.set(Calendar.YEAR, year);

        Date currYearFirst = calendar.getTime();

        return currYearFirst;

    }

}
